/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.service.impl;

import java.util.List;
import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.mapper.SysRoleMapper;
import com.ktjiaoyu.utils.Page;
import com.ktjiaoyu.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author 科泰集团教研团队
 * @packageName com.ktjiaoyu
 * @fileName SysRole.java
 * @createTime 2024/10/22 09:37:00
 * @lastModify 2024/10/22 09:37:00
 * @version 1.0.0
 */
@Service
public class SysRoleServiceImpl implements SysRoleService{

    private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Autowired
    private SysRoleMapper sysRoleMapper;

    public Page<SysRole> findPage(SysRole entity, Integer pageNo, Integer pageSize)  throws Exception{
        Page<SysRole> page = null;
        try {
           page = new Page<SysRole>();
           page.setPageSize(pageSize);
           // 查询总数，计算总页数
           Long count = sysRoleMapper.count(entity);
           page.setTotalCount(count.intValue());
           page.setPageNo(pageNo);
           // 查询分页数据
           List<SysRole> data = sysRoleMapper.getPage(entity, page.offset(), page.getPageSize());
           page.setData(data);
       } catch (Exception e) {
            LOGGER.error("The class SysRoleServiceImpl's function [findPage(SysRole, Integer, Integer)]  occurred errors.", e);
            throw e;
        }
        return page;
    }

    public List<SysRole> findAll() throws Exception{
        try {
           return sysRoleMapper.getAll();
        } catch (Exception e) {
            LOGGER.error("The class SysRoleServiceImpl's function [findAll()]  occurred errors.", e);
            throw e;
        }
    }

    public SysRole findById(Long id) throws Exception{
        try {
           return sysRoleMapper.getById(id);
        } catch (Exception e) {
            LOGGER.error("The class SysRoleServiceImpl's function [findById(Long)]  occurred errors.", e);
            throw e;
        }
    }


    @Override
    public boolean saveOrUpdate(SysRole entity) throws Exception {
        try {
            if(entity.getId() == null){
                // 无主键则新增
                return sysRoleMapper.insert(entity) > 0;
            }else{
                // 有主键则修改
                return sysRoleMapper.update(entity) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("The class SysRoleServiceImpl's function [saveOrUpdate(SysRole)]  occurred errors.", e);
            throw e;
        }
    }

    public boolean remove(Long id) throws Exception{
        try {
            return sysRoleMapper.delete(id) > 0;
        } catch (Exception e) {
            LOGGER.error("The class SysRoleServiceImpl's function [delete(Long)]  occurred errors.", e);
            throw e;
        }
    }

    @Override
    public SysRole findByCode(String code) {
        try {
            return sysRoleMapper.getByCode(code);
        } catch (Exception e) {
            LOGGER.error("The class SysRoleServiceImpl's function [findByCode(String)]  occurred errors.", e);
            throw e;
        }
    }

    @Override
    public Long countRoleUsers(Long id) {
        try {
            return sysRoleMapper.countRoleUsers(id);
        } catch (Exception e) {
            LOGGER.error("The class SysRoleServiceImpl's function [countRoleUsers(Long)]  occurred errors.", e);
            throw e;
        }
    }

}
